const app = getApp()
const WXAPI = require("../../../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    billList:[],
    typeShow: false,
    statusShow:false,
    typeLs:[
      
    ],
    statusLs:[
     
    ],
    pageSize: 10,
    pageNum: 1,
    total: 0,
    lodingtype: 1,//加载状态（0：加载中  1：加载完成没有更多）
    imgHttp: app.globalData.imgHttp, //图片前缀
    giveType: '',//赠送类型
    assetsStatus:'', //状态
    changeIndex1: 0,
    changeIndex2: 0,
    isPull: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight:app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom
    })
    this.getdict()
    this.getdictStatus()
    this.getLs()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageNum:1
    })
    // this.getLs()
  },
  //下拉刷新
  onPullDownRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      pageNum:1
    })
    this.data.giveType = ''
    this.data.assetsStatus = ''
    this.getLs();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isPull==true){
      var pageNum = this.data.pageNum
      pageNum ++
      console.log("触底了",pageNum)
      this.setData({
        pageNum: pageNum
      });
      this.getLs()
    
  }
  },

  back(){
    wx.navigateBack({
      delta: 0,
    })
  },
  getdict(){ //订单类型字典
    var data = {
      dictType: 'customer_give_manage_type ',
    }
    WXAPI.dict(data, (res) => {
      var arr = [
        {
          id: "",
          value: "不限",
        }
      ]
      let options = arr.concat(res.result[0].dictList)
      const data = res.result
      if (res.code == 200) {
        this.setData({
          typeLs: options
        })
      }
    })
  },
  getdictStatus(){ //订单状态字典
    var data = {
      dictType: 'customer_give_order_status  ',
    }
    WXAPI.dict(data, (res) => {
      var arr = [
        {
          id: "",
          value: "不限",
        }
      ]
      let options = arr.concat(res.result[0].dictList)
      console.log(options)
      const data = res.result
      if (res.code == 200) {
        this.setData({
          statusLs: options
        })
      }
    })
  },
  changeshowType(){ //订单类型选择打开
    // let show = false
    this.data.typeShow = !this.data.typeShow
    this.setData({
      typeShow:this.data.typeShow,
      statusShow: false
     
    })
  },
  changeshowStatus(){ //订单状态打开
    this.data.statusShow = !this.data.statusShow
    this.setData({
      statusShow:this.data.statusShow,
      typeShow: false
    })
  },
  close(){
    this.setData({
      statusShow: false,
      typeShow: false
    })
  },
  checkType(e){ //类型选择
    console.log(e)
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    this.data.giveType = id
    this.setData({
      statusShow: false,
      pageNum:1,
      typeShow: false,
      changeIndex1: index,
      billList:[],
    })
    this.getLs()
  },
  checkStatus(e){ //状态选择
    console.log(e)
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    this.data.assetsStatus = id
    this.setData({
      statusShow: false,
      pageNum:1,
      typeShow: false,
      changeIndex2: index,
      billList:[],
    })
    this.getLs()
  },
  getLs(){ //transferRecord
    wx.showLoading({
      title: '加载中',
    })
    var data = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      giveType: this.data.giveType,
      status: this.data.assetsStatus
    }
    WXAPI.transferRecord(data, (res) => {
      const {size,total,records} = res.result
      if (res.code == 200) {
        wx.hideNavigationBarLoading();
        // if(res.result.records.length==0){
        //   this.setData({
        //     list:[]
        //   })
        // }
        // this.data.total = res.result.total
        var billList = this.data.billList
        if(billList.length == 0){
          billList = billList.concat(records)
        }else{
          billList.map(nav =>{
            records.map((item,index) =>{
                if(nav.groupTime == item.groupTime){
                  console.log('item-1',item.children)
                  nav.children = nav.children.concat(item.children)
                  return nav.children
                }
                else{
                  billList = billList.concat(item)
                  return billList
                }
            })
          })
        }
        let hash = {};
        billList = billList.reduce((preVal, curVal) => {
          console.log(' hash[curVal.groupTime]', hash[curVal.groupTime])
          hash[curVal.groupTime] ? '' : hash[curVal.groupTime] = true && preVal.push(curVal);
          return preVal
        }, [])
        console.log('billList',billList)
        if(this.data.pageNum >= Math.ceil(total / size)){
          console.log('没有分页+++++++++++')
            this.setData({
              lodingtype: 1,
              isPull: false
            })
        }else{
          this.setData({
            lodingtype: 0
          })
        }
        this.setData({
          billList: billList
        })
       
        wx.hideNavigationBarLoading(()=>{
          console.log(111)
        });
        wx.hideLoading({})
      }
    })
  },
  chuli(arrLs){
    billList.map(list =>{
      records.map(item=>{
        if(item.groupTime == list.groupTime){
          list.children = list.children.concat(item.children)
          return list.children
        }else{
          billList = billList.concat(item)
          return billList
        }
      })
      // console.log('item-1',item)
    })
    return arr;
  },
  jump(e){ //跳转详情
    console.log(e.currentTarget.dataset.index)
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    console.log(name)
    let status = 0
    if(name=='' && e.currentTarget.dataset.status !=2 && e.currentTarget.dataset.status !=1 && e.currentTarget.dataset.status !=4){
      status = 6
    }else{
      status = e.currentTarget.dataset.status
    }
    if(e.currentTarget.dataset.index==1){
      wx.navigateTo({
        url: '/userPageA/pages/wallet/record_detailGive/record_detailGive?id=' + id + '&status=' + status,
      })
    }else{
      wx.navigateTo({
        url: '/userPageA/pages/wallet/record_detailReceive/record_detailReceive?id=' + id,
      })
    }
  },
})